package com.shaobufan.museum.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shaobufan.museum.mapper.ScheduleMapper;
import com.shaobufan.museum.pojo.entity.Schedule;
import com.shaobufan.museum.pojo.vo.ScheduleVo;
import com.shaobufan.museum.pojo.vo.UserScheduleVo;
import com.shaobufan.museum.service.ScheduleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 17126
 * @description 针对表【schedule(日程表)】的数据库操作Service实现
 * @createDate 2024-03-11 21:28:55
 */
@Service
public class ScheduleServiceImpl extends ServiceImpl<ScheduleMapper, Schedule>
        implements ScheduleService {

    @Resource
    ScheduleMapper scheduleMapper;

    /**
     * 检索所有日程类别
     *
     * @return 类别 List
     */
    @Override
    public List<ScheduleVo> searchAllSchedule() {
        List<Schedule> scheduleList = scheduleMapper.selectList(null);
        List<ScheduleVo> scheduleVoList;
        scheduleVoList = scheduleList.stream().map(this::getScheduleVo).collect(Collectors.toList());
        return scheduleVoList;
    }

    /**
     * 检索符合的日程类别
     *
     * @return 类别 List
     */
    @Override
    public ScheduleVo searchSchedule(Integer scheduleId) {
        QueryWrapper<Schedule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("schedule_id", scheduleId);
        Schedule schedule = scheduleMapper.selectOne(queryWrapper);
        return getScheduleVo(schedule);
    }


    /**
     * 获取用户的日程
     *
     * @param userScheduleVoList 用户日程表对应关系列表
     * @return 用户的日程
     */
    @Override
    public List<ScheduleVo> getUserSchedule(List<UserScheduleVo> userScheduleVoList) {
        List<ScheduleVo> scheduleVoList = new ArrayList<>();
        for (UserScheduleVo userScheduleVo : userScheduleVoList) {
            ScheduleVo scheduleVo = searchSchedule(userScheduleVo.getScheduleId());
            scheduleVoList.add(scheduleVo);
        }
        return scheduleVoList;
    }

    /**
     * 获取scheduleVo
     *
     * @param schedule 日程实体
     * @return scheduleVo 日程实体Vo
     */
    @Override
    public ScheduleVo getScheduleVo(Schedule schedule) {
        ScheduleVo scheduleVo = new ScheduleVo();
        scheduleVo.setScheduleId(schedule.getScheduleId());
        scheduleVo.setScheduleName(schedule.getScheduleName());
        return scheduleVo;
    }
}